package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.UserMapper;
import cn.tedu.egcar1.pojo.dto.UserLoginParam;
import cn.tedu.egcar1.pojo.dto.UserRegParam;
import cn.tedu.egcar1.pojo.entity.User;
import cn.tedu.egcar1.pojo.vo.UserLoginVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.PanelUI;
import java.util.Date;

@Slf4j
@Tag(name="用户管理")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order=10)
    public JsonResult regUser(@RequestBody UserRegParam param){
      log.debug("注册信息："+param);
      int rows=userMapper.selectByUsername(param);
      if (rows>0){
          return new JsonResult(StatusCode.USERNAME_EXISTS);
      }
      User user=new User();
      BeanUtils.copyProperties(param,user);
      user.setCreate_time(new Date());
      user.setUpdate_time(new Date());
      userMapper.insertUser(user);
      return JsonResult.OK();
    }

    @PostMapping("login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult loginUser(@RequestBody UserLoginParam param){
        log.debug("登录参数:"+param);
        UserLoginVO loginVo=userMapper.selectByUsernameAndPassword(param);
        if (loginVo !=null){
            return JsonResult.OK(loginVo);
        }else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }


}
